<%@ page contentType="text/html;charset=UTF-8" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <link rel="stylesheet" type="text/css" href="${resource(dir:'css',file:'print.css')}" media="print" />
        <script type="text/javascript">
            //Imprimir
            function imprimir(){
              self.print();
            }
        </script>
        <title>Relatório</title>
    </head>
    <body>
        <div class="cabelistlateral">
          <img src="${resource(dir:'images',file:'seta_cabecalho_20x20.png')}" /> Pesquisar Convênios
          <img src="${resource(dir:'images',file:'ico_print.png')}" alt="Imprimir Relatório" title="Imprimir Relatorio"  style="float:right; cursor:hand; cursor:pointer" onclick="imprimir();"/>
        </div><br/>
        <div class="pesquisa">
            <%
            String nome      = "";
            Date   dtDataIni = new Date();
            Date   dtDataFim = new Date();

            if (buscaInstance) {
                nome      = buscaInstance.nome;
                dtDataIni = new Date(buscaInstance.strDataIni);
                dtDataFim = new Date(buscaInstance.strDataFim);
            }
            %>
            <form method="post" action="/EstagioWeb/relatorio/pesquisarConvenio">
              <div class="itens">Razão Social / Nome <br/> <input class="text" type="text" name="nome" value="${nome}" size="30"/></div>
              <div class="itens">Data Início <br/> <g:datePicker name="dtDataIni" value="${dtDataIni}" precision="day"/></div>
              <div class="itens">Data Fim <br/> <g:datePicker name="dtDataFim" value="${dtDataFim}" precision="day"/></div>
              <div class="itens btsalvarcadastro" style="margin:6px 0 0 0;">
                  <input class="ui-corner-all" type="submit" name="buscar" value="BUSCAR CONVÊNIOS"/>
              </div>
            </form>
            <br class="clear"/>
        </div>
        <div class="list">
            <table>
                <thead>
                    <tr>
                        <td>Razão Social</td>
                        <td>Nome</td>
                        <td class="ico">Data Término</td>
                    </tr>
                </thead>
                <tbody>
                <g:each in="${unidadeConcedenteInstanceList}" status="i" var="unidadeConcedenteInstance">
                    <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
                        <%
                        def pessoaInstance       = new Pessoa();
                        def pessoaInstanceList   = Pessoa.findById(unidadeConcedenteInstance.id);

                        def convenioInstance     = Convenio.createCriteria();

                        def convenioInstanceList;

                        if (buscaInstance.dtDataIni == null || buscaInstance.dtDataFim == null) {
                           convenioInstanceList = convenioInstance {
                              eq("colaborador", unidadeConcedenteInstance )
                              maxResults(1)
                              order("dtTermino", "desc")
                          };
                        } else {
                          convenioInstanceList = convenioInstance.list() {
                              eq("colaborador", unidadeConcedenteInstance)
                              maxResults(1)
                              between("dtTermino", buscaInstance.dtDataIni, buscaInstance.dtDataFim)
                              order("dtTermino", "desc")
                          };
                        }

                        if (convenioInstanceList) {
                        %>
                          <td>${fieldValue(bean: unidadeConcedenteInstance, field: "razaoSocial")}</td>
                          <td>${pessoaInstanceList.nome}</td>
                          <%
                             Date dt_hoje = new Date() + 180;
                             if (convenioInstanceList.dtTermino[0] < dt_hoje) {
                          %>
                            <td style="color:red"><g:formatDate date ="${convenioInstanceList.dtTermino[0]}" format="dd/MM/yyyy"/></td>
                          <% } else { 
                          %>
                            <td style="color:blue"><g:formatDate date ="${convenioInstanceList.dtTermino[0]}" format="dd/MM/yyyy"/></td>
                          <% } %>
                        <%
                        }
                        %>
                    </tr>
                </g:each>
                </tbody>
            </table>
        </div>
    </body>
</html>